/* Y o u r   D e s c r i p t i o n                       */
/*                            AppBuilder Photon Code Lib */
/*                                         Version 2.01  */

/* Standard headers */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>

/* Local headers */
#include "ablibs.h"
#include "global.h"
#include "abimport.h"
#include "proto.h"

extern char *ep1_addr [5];
extern char *ep2_addr [5];

int
trend_arch_setup( PtWidget_t *link_instance, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
	unsigned short *ppi;	
	int *flags;
	
	/* eliminate 'unreferenced' warnings */
	link_instance = link_instance, apinfo = apinfo, cbinfo = cbinfo;

	PtGetResource (ABW_Pause1, Pt_ARG_FLAGS, &flags, 0);
	*flags |= Pt_SET;
	PtGetResource (ABW_Pause2, Pt_ARG_FLAGS, &flags, 0);
	*flags |= Pt_SET;

	PtGetResource (ABW_PtPanelGroup1, Pt_ARG_PG_CURRENT_INDEX, &ppi, 0);
	switch (*ppi) {
		case 1:
			PtSetResource (ABW_CwTrend_arch, TREND_ARG_ADDR, 1, 0);
			PtSetResource (ABW_CwTrend_arch, TREND_ARG_HIST_NAME, ep1_addr, 5);
			PtSetResource (ABW_arch_opt_ep_combo, Pt_ARG_CBOX_SEL_ITEM, 1, 0);
			PtSetResource (ABW_CwTrend_arch, TREND_ARG_LOAD_DATA, 0, 0);
			PtDamageWidget (ABW_CwTrend_arch);
			break;	
		case 2:
			PtSetResource (ABW_CwTrend_arch, TREND_ARG_ADDR, 2, 0);
			PtSetResource (ABW_CwTrend_arch, TREND_ARG_HIST_NAME, ep2_addr, 5);
			PtSetResource (ABW_arch_opt_ep_combo, Pt_ARG_CBOX_SEL_ITEM, 2, 0);
			PtSetResource (ABW_CwTrend_arch, TREND_ARG_LOAD_DATA, 0, 0);
			PtDamageWidget (ABW_CwTrend_arch);
			break;	
	}

	return( Pt_CONTINUE );
}

int
arch_opt_ok( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
	int i;
	double min_y [5];
	double max_y [5];
	PtWidget_t *toggle [5] 	= {ABW_toggle1, ABW_toggle2, ABW_toggle3, ABW_toggle4, ABW_toggle5};
	PtWidget_t *min [5] 	= {ABW_min1, ABW_min2, ABW_min3, ABW_min4, ABW_min5};
	PtWidget_t *max [5] 	= {ABW_max1, ABW_max2, ABW_max3, ABW_max4, ABW_max5};
	unsigned short mask;
	long *flags;
	double *value;
	int *ivalue;
	time_t time_win, time_beg;
	time_t *time_end;
	time_t *prv_time_win;

	/* eliminate 'unreferenced' warnings */
	widget = widget, apinfo = apinfo, cbinfo = cbinfo;

	mask = 0;
	for (i=0; i< 5; i++) {
		PtGetResource (toggle [i], Pt_ARG_FLAGS, &flags, 0);
		if ((*flags)& Pt_SET) mask |= (1<< i);		
		PtGetResource (max [i], Pt_ARG_NUMERIC_VALUE, &value, 0);
		max_y [i] = *value;
		PtGetResource (min [i], Pt_ARG_NUMERIC_VALUE, &value, 0);
		min_y [i] = *value;
	}
	if (mask == 0) mask = 1;

	PtSetResource (ABW_CwTrend_arch, TREND_ARG_AXIS_MASK, mask, 0);
	PtSetResource (ABW_CwTrend_arch, TREND_ARG_AXIS_MINY, min_y, 5);
	PtSetResource (ABW_CwTrend_arch, TREND_ARG_AXIS_MAXY, max_y, 5);

	PtGetResource (ABW_razv_hour, Pt_ARG_NUMERIC_VALUE, &ivalue, 0);
	time_win = (*ivalue)* 3600;
	PtGetResource (ABW_razv_min, Pt_ARG_NUMERIC_VALUE, &ivalue, 0);
	time_win += (*ivalue)* 60;
	PtGetResource (ABW_razv_sec, Pt_ARG_NUMERIC_VALUE, &ivalue, 0);
	time_win += *ivalue;

	PtGetResource (ABW_CwTrend_arch, TREND_ARG_TIME_END, &time_end, 0);
	PtGetResource (ABW_CwTrend_arch, TREND_ARG_TIME_WIN, &prv_time_win, 0);
	time_beg = *time_end - *prv_time_win;
	*time_end = time_beg + time_win;	
	PtSetResource (ABW_CwTrend_arch, TREND_ARG_TIME_WIN, time_win, 0);

	PtSetResource (ABW_CwTrend_arch, TREND_ARG_LOAD_DATA, 0, 0);
	PtDamageWidget (ABW_CwTrend_arch);

	return( Pt_CONTINUE );
}


int
arch_opt_setup( PtWidget_t *link_instance, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
	char **labels;
	double *min_y;
	double *max_y;
	char str [255];
	int i, j;
	PtWidget_t *toggle [5] 	= {ABW_toggle1, ABW_toggle2, ABW_toggle3, ABW_toggle4, ABW_toggle5};
	PtWidget_t *min [5] 	= {ABW_min1, ABW_min2, ABW_min3, ABW_min4, ABW_min5};
	PtWidget_t *max [5] 	= {ABW_max1, ABW_max2, ABW_max3, ABW_max4, ABW_max5};
	unsigned short *mask;
	long *flags;
	time_t *time_win;
	int hours, minutes, seconds;
	int data;

	/* eliminate 'unreferenced' warnings */
	link_instance = link_instance, apinfo = apinfo, cbinfo = cbinfo;

	switch (apinfo->reason) {
		case ABR_PRE_REALIZE:
			PtGetResource (ABW_CwTrend_arch, TREND_ARG_AXIS_LABELS, &labels, 0);
			PtGetResource (ABW_CwTrend_arch, TREND_ARG_AXIS_MINY, &min_y, 0);
			PtGetResource (ABW_CwTrend_arch, TREND_ARG_AXIS_MAXY, &max_y, 0);
			PtGetResource (ABW_CwTrend_arch, TREND_ARG_TIME_WIN, &time_win, 0);
			for (i=0; i< 5; i++) {
				strcpy (str, labels [i]);
				for (j=0; j< strlen (str); j++) {
					if (str [j] == '\n') str [j] = ',';
				}	
				PtSetResource (toggle [i], Pt_ARG_TEXT_STRING, str, 0);
	
				PtGetResource (ABW_CwTrend_arch, TREND_ARG_AXIS_MASK, &mask, 0);
				PtGetResource (toggle [i], Pt_ARG_FLAGS, &flags, 0);
				if (((*mask)>> i)&1) *flags |= Pt_SET;
				else *flags &= ~Pt_SET; 
				
				PtSetResource (min [i], Pt_ARG_NUMERIC_VALUE, &min_y [i], 0);
				PtSetResource (max [i], Pt_ARG_NUMERIC_VALUE, &max_y [i], 0);
			}
			hours = *time_win/3600;
			PtSetResource (ABW_razv_hour, Pt_ARG_NUMERIC_VALUE, hours, 0);
			minutes = (*time_win - hours*3600)/60;
			PtSetResource (ABW_razv_min, Pt_ARG_NUMERIC_VALUE, minutes, 0);
			seconds = *time_win - (hours*3600 + minutes*60);
			PtSetResource (ABW_razv_sec, Pt_ARG_NUMERIC_VALUE, seconds, 0);
			
//			PtRemoveCallback (ABW_opt_ok, Pt_CB_ACTIVATE, (PtCallbackF_t *) trend_opt_done, NULL );
//			PtAddCallback (ABW_opt_ok, Pt_CB_ACTIVATE, (PtCallbackF_t *) arch_opt_ok, NULL);

			data = 3;
			PtSetResource (ABW_opt_ok, Pt_ARG_USER_DATA, &data, sizeof (data));

			break;
	}

	return( Pt_CONTINUE );
}

int
arch_beg_setup( PtWidget_t *link_instance, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
	const char *month_names [12] 	= {	"Январь", 	"Февраль",	"Март",		"Апрель",
										"Май",		"Июнь",		"Июль",		"Август",
										"Сентябрь",	"Октябрь",	"Ноябрь",	"Декабрь"};
	const char *wday_names [7] 		= {	"Вс", "Пн", "Вт", "Ср", "Чт", "Пт", "Сб"};
	time_t *time_win;
	time_t *time_end;
	time_t time_beg;
	struct tm *tmp;

	/* eliminate 'unreferenced' warnings */
	link_instance = link_instance, apinfo = apinfo, cbinfo = cbinfo;

	switch (apinfo->reason) {
		case ABR_PRE_REALIZE:
			PtSetResource (ABW_date_beg, Pt_ARG_CALENDAR_MONTH_NAMES, month_names, 12);
			PtSetResource (ABW_date_beg, Pt_ARG_CALENDAR_WDAY_NAMES,  wday_names,  7);		
			PtSetResource (ABW_date_beg, Pt_ARG_CALENDAR_HIGHLIGHT, 0x41, 0);		
			PtSetResource (ABW_date_beg, Pt_ARG_CALENDAR_COLOR4, Pg_RED, 0);		

			PtGetResource (ABW_CwTrend_arch, TREND_ARG_TIME_WIN, &time_win, 0);
			PtGetResource (ABW_CwTrend_arch, TREND_ARG_TIME_END, &time_end, 0);
			time_beg = *time_end - *time_win;

			PtSetResource (ABW_date_beg, Pt_ARG_CALENDAR_TIME_T, time_beg, 0);

			tmp = localtime (&time_beg);
			PtSetResource (ABW_min_beg, Pt_ARG_NUMERIC_VALUE, tmp->tm_min, 0);
			PtSetResource (ABW_hour_beg, Pt_ARG_NUMERIC_VALUE, tmp->tm_hour, 0);
	
			break;	
	}

	return( Pt_CONTINUE );
}


int
arch_beg_ok( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
	time_t *time_beg;
	time_t *time_win;
	time_t time_end;
	int *ivalue;

	/* eliminate 'unreferenced' warnings */
	widget = widget, apinfo = apinfo, cbinfo = cbinfo;

	PtGetResource (ABW_CwTrend_arch, TREND_ARG_TIME_WIN, &time_win, 0);
	PtGetResource (ABW_date_beg, Pt_ARG_CALENDAR_TIME_T, &time_beg, 0);	
	PtGetResource (ABW_hour_beg, Pt_ARG_NUMERIC_VALUE, &ivalue, 0);
	time_end = *time_beg + (*ivalue)* 3600 + *time_win;
	PtGetResource (ABW_min_beg, Pt_ARG_NUMERIC_VALUE, &ivalue, 0);
	time_end += (*ivalue)* 60;
	PtSetResource (ABW_CwTrend_arch, TREND_ARG_TIME_END, time_end, 0);
	PtSetResource (ABW_CwTrend_arch, TREND_ARG_LOAD_DATA, 0, 0);
	PtDamageWidget (ABW_CwTrend_arch);

	return( Pt_CONTINUE );
}


int
arch_ep_combo( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
	unsigned short *selected;

	/* eliminate 'unreferenced' warnings */
	widget = widget, apinfo = apinfo, cbinfo = cbinfo;

	PtGetResource (ABW_arch_opt_ep_combo, Pt_ARG_CBOX_SEL_ITEM, &selected, 0);
	switch (*selected) {
		case 1:
			PtSetResource (ABW_CwTrend_arch, TREND_ARG_ADDR, 1, 0);
			PtSetResource (ABW_CwTrend_arch, TREND_ARG_HIST_NAME, ep1_addr, 5);
			PtSetResource (ABW_arch_opt_ep_combo, Pt_ARG_CBOX_SEL_ITEM, 1, 0);
			PtSetResource (ABW_CwTrend_arch, TREND_ARG_LOAD_DATA, 0, 0);
			PtDamageWidget (ABW_CwTrend_arch);
			break;	
		case 2:
			PtSetResource (ABW_CwTrend_arch, TREND_ARG_ADDR, 2, 0);
			PtSetResource (ABW_CwTrend_arch, TREND_ARG_HIST_NAME, ep2_addr, 5);
			PtSetResource (ABW_arch_opt_ep_combo, Pt_ARG_CBOX_SEL_ITEM, 2, 0);
			PtSetResource (ABW_CwTrend_arch, TREND_ARG_LOAD_DATA, 0, 0);
			PtDamageWidget (ABW_CwTrend_arch);
			break;	
	}

	return( Pt_CONTINUE );
}


int
print_arch( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
	PpPrintContext_t *pc;
//	PhDrawContext_t *dc;
	PhArea_t area;
    int action;
//    char orientation = 2;

	/* eliminate 'unreferenced' warnings */
	widget = widget, apinfo = apinfo, cbinfo = cbinfo;

	//PtFlush ();

    /* Create a print context. */
    pc = PpCreatePC ();
    action = PtPrintSelection (ABW_trend_arch, NULL, 
                              "Demo Print Selector", 
                              pc, Pt_PRINTSEL_DFLT_LOOK);

	if (action != Pt_PRINTSEL_CANCEL)
    {
        /* Start printing the label. */
        PpStartJob(pc);
        PpContinueJob(pc);

		PtWidgetArea (ABW_CwTrend_arch, &area);
        PpSetPC (pc, Pp_PC_SOURCE_SIZE, &area.size, 0);	
//        PpSetPC (pc, Pp_PC_ORIENTATION, &orientation, 0);	        	
        /* Print the widget. */
		//dc = PhDCSetCurrent (pc);
		PtSetResource (ABW_CwTrend_arch, TREND_ARG_PRINTING, 1, 0);
		PpPrintWidget (pc, ABW_CwTrend_arch, &area.pos, NULL, Pt_PP_RESIZE_PC);
		PtSetResource (ABW_CwTrend_arch, TREND_ARG_PRINTING, 0, 0);
		//PtDamageWidget (ABW_CwTrend_arch);
        PgFlushCx (pc);
        PtFlush ();
   		//PhDCSetCurrent (dc);
        /* Close the PC. */
        PpSuspendJob(pc);
        PpEndJob(pc);
    }	

	PpReleasePC (pc);
	pc = NULL;

	return( Pt_CONTINUE );
}

int
trend_arch_done( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
	int *flags;

	/* eliminate 'unreferenced' warnings */
	widget = widget, apinfo = apinfo, cbinfo = cbinfo;

	PtGetResource (ABW_Pause1, Pt_ARG_FLAGS, &flags, 0);
	*flags &= ~Pt_SET;
	PtGetResource (ABW_Pause2, Pt_ARG_FLAGS, &flags, 0);
	*flags &= ~Pt_SET;

	return( Pt_CONTINUE );
}
